home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-07-09 | 13.3 KB | 466 lines | [TEXT/CWIE] |
- // =================================================================================
- // CPict2AsciiApp.cp 1997 BB's Team inc. All rights reserved
- // =================================================================================
- #include "PLConstants.h"
- #include "CPreferences.h"
- #include "CDocument.h"
- #include "CPict2AsciiApp.h"
-
- #include "CDynamicEditField.h"
- #include "CMemoryIndicator.h"
- #include "LDynamicPopupMenu.h"
- #include "CDynamicText.h"
- #include "CGreyCaption.h"
- #include "CGWorldPane.h"
-
- // StandardGetFilePreview
- #include <ImageCompression.h>
-
- #include <LGrowZone.h>
- #include <LMenu.h>
- #include <LMenuBar.h>
- #include <LStdControl.h>
- #include <LString.h>
- #include <UDesktop.h>
- #include <UDrawingState.h>
- #include <UMemoryMgr.h>
- #include <LDialogBox.h>
- #include <UReanimator.h>
- #include <URegistrar.h>
- #include <LEditField.h>
- #include <LPrintOut.h>
- #include <LRadioGroup.h>
- #include <LActiveScroller.h>
- #include <LGroupBox.h>
-
- #include <PP_Messages.h>
-
- #include <UAppleEventsMgr.h> // MJS 97-05-29
- #include <UExtractFromAEDesc.h> // MJS 97-05-29
-
-
- #ifdef PL_DEBUG
- #include <LSIOUXAttachment.h>
- #endif
-
- // =================================================================================
- // • Main Program
- // =================================================================================
- void
- main( void )
- {
- // Initialize the heap. Parameter is number
- // of master handle blocks to allocate.
- InitializeHeap ( 4 );
-
- // Initialize the MacOS toolbox.
- UQDGlobals::InitializeToolbox( &qd );
-
- // Setup the throw and signal actions.
- #ifdef PL_DEBUG
- SetDebugThrow_( debugAction_Alert );
- SetDebugSignal_( debugAction_Alert );
- #endif
-
- // Install a GrowZone function to catch low memory situations.
- // Parameter is the size of the memory reserve in bytes.
- new LGrowZone( 30000 );
-
- // Create the application object and run it.
- CPict2AsciiApp theApp;
- theApp.Run();
- }
-
-
- // ---------------------------------------------------------------------------------
- // • CPict2AsciiApp
- // ---------------------------------------------------------------------------------
- CPict2AsciiApp::CPict2AsciiApp()
- {
- // Register PowerPlant class creator functions.
- URegistrar::RegisterClass(LCaption::class_ID, (ClassCreatorFunc) LCaption::CreateCaptionStream);
- URegistrar::RegisterClass(LDialogBox::class_ID, (ClassCreatorFunc) LDialogBox::CreateDialogBoxStream);
- URegistrar::RegisterClass(LEditField::class_ID, (ClassCreatorFunc) LEditField::CreateEditFieldStream);
- URegistrar::RegisterClass(LPane::class_ID, (ClassCreatorFunc) LPane::CreatePaneStream);
- URegistrar::RegisterClass(LPlaceHolder::class_ID, (ClassCreatorFunc) LPlaceHolder::CreatePlaceHolderStream);
- URegistrar::RegisterClass(LPrintout::class_ID, (ClassCreatorFunc) LPrintout::CreatePrintoutStream);
- URegistrar::RegisterClass(LStdButton::class_ID, (ClassCreatorFunc) LStdButton::CreateStdButtonStream);
- URegistrar::RegisterClass(LStdCheckBox::class_ID, (ClassCreatorFunc) LStdCheckBox::CreateStdCheckBoxStream);
- URegistrar::RegisterClass(LStdRadioButton::class_ID,(ClassCreatorFunc) LStdRadioButton::CreateStdRadioButtonStream);
- URegistrar::RegisterClass(LStdPopupMenu::class_ID, (ClassCreatorFunc) LStdPopupMenu::CreateStdPopupMenuStream);
- URegistrar::RegisterClass(LWindow::class_ID, (ClassCreatorFunc) LWindow::CreateWindowStream);
-
- URegistrar::RegisterClass(LRadioGroup::class_ID, (ClassCreatorFunc) LRadioGroup::CreateRadioGroupStream);
- URegistrar::RegisterClass(LActiveScroller::class_ID,(ClassCreatorFunc) LActiveScroller::CreateActiveScrollerStream);
- URegistrar::RegisterClass(LGroupBox::class_ID, (ClassCreatorFunc) LGroupBox::CreateGroupBoxStream);
-
- // Register custom classes.
- URegistrar::RegisterClass( CDynamicText::class_ID, (ClassCreatorFunc) CDynamicText::CreateDynamicTextStream );
- URegistrar::RegisterClass( CGreyCaption::class_ID, (ClassCreatorFunc) CGreyCaption::CreateGreyCaptionStream );
- URegistrar::RegisterClass( CGWorldPane::class_ID, (ClassCreatorFunc) CGWorldPane::CreateGWorldPaneStream );
- URegistrar::RegisterClass(LDynamicPopupMenu::class_ID, (ClassCreatorFunc) LDynamicPopupMenu::CreateLDynamicPopupMenuStream );
- URegistrar::RegisterClass( CMemoryIndicator::class_ID, (ClassCreatorFunc) CMemoryIndicator::CreateMemoryIndicatorStream );
- URegistrar::RegisterClass(CDynamicEditField::class_ID, (ClassCreatorFunc) CDynamicEditField::CreateDynamicEditFieldStream );
-
- #ifdef PL_DEBUG
- AddAttachment(new LSIOUXAttachment); // *** Use SIOUX Attachment
- #endif
- mPreferences.FinishCreate();
- }
-
-
- // ---------------------------------------------------------------------------------
- // • ~CPict2AsciiApp
- // ---------------------------------------------------------------------------------
- CPict2AsciiApp::~CPict2AsciiApp()
- {
- }
-
-
- // ---------------------------------------------------------------------------------
- // • StartUp
- // ---------------------------------------------------------------------------------
- void
- CPict2AsciiApp::StartUp()
- {
- // ObeyCommand (cmd_Open, nil );
- }
-
-
- // ---------------------------------------------------------------------------------
- // • FindCommandStatus
- // ---------------------------------------------------------------------------------
- void
- CPict2AsciiApp::FindCommandStatus(
- CommandT inCommand,
- Boolean &outEnabled,
- Boolean &outUsesMark,
- Char16 &outMark,
- Str255 outName )
- {
- switch ( inCommand ) {
-
- case cmd_New:
- outEnabled = false;
- break;
-
- case cmd_Preferences:
- outEnabled = true;
- break;
-
- default:
- {
- // Call inherited.
- LDocApplication::FindCommandStatus( inCommand,
- outEnabled, outUsesMark, outMark, outName );
- }
- break;
-
- }
- }
-
-
-
- // ---------------------------------------------------------------------------
- // • ObeyCommand
- // ---------------------------------------------------------------------------
- // Respond to commands
-
- Boolean
- CPict2AsciiApp::ObeyCommand(
- CommandT inCommand,
- void *ioParam)
- {
- Boolean cmdHandled = true;
-
- switch (inCommand) {
-
- case cmd_Preferences:
- mPreferences.DoDialog (mPreferences.GetAllowed());
-
- // No active menus when back from dialog ?
- // In fact, no target at all !
- SwitchTarget(this);
- break;
-
- case cmd_PageSetup:
- SetupPage();
- break;
-
- default:
- cmdHandled = LDocApplication::ObeyCommand(inCommand, ioParam);
- break;
- }
-
- return cmdHandled;
- }
-
-
- // ---------------------------------------------------------------------------------
- // • OpenDocument (Called in response to the ODOC event)
- // ---------------------------------------------------------------------------------
- void
- CPict2AsciiApp::OpenDocument(
- FSSpec *inMacFSSpec)
- {
- new CDocument (this, inMacFSSpec, &mPreferences);
- }
-
-
- // ---------------------------------------------------------------------------------
- // • ChooseDocument (called by the "open" menu item)
- // ---------------------------------------------------------------------------------
- void
- CPict2AsciiApp::ChooseDocument()
- {
- // Deactivate the desktop.
- UDesktop::Deactivate();
-
- // Browse for a document.
- SFTypeList theList = { 'PICT' };
- StandardFileReply theReply;
- ::StandardGetFilePreview (nil, 1, theList, &theReply);
-
- // Reactivate the desktop.
- UDesktop::Activate();
-
- // Send an apple event to open the file.
- if (theReply.sfGood)
- SendAEOpenDoc (theReply.sfFile);
-
- }
-
-
- // ---------------------------------------------------------------------------------
- // • Additional AppleScript support (Michael Schuerig 97-05-29)
- // ---------------------------------------------------------------------------------
-
- #define ae_pict2ascii 10000
-
- #define pZoomFactor 'zoom'
- #define pAutoContrast 'cntr'
- #define pOptimizedForPrint 'oprt'
- #define pCharSet 'cset'
- #define pMonoSpace 'mosp'
- #define pAllow8Bit '8bit'
-
-
- // ---------------------------------------------------------------------------------
- // • HandleAppleEvent
- // ---------------------------------------------------------------------------------
-
- void
- CPict2AsciiApp::HandleAppleEvent(
- const AppleEvent &inAppleEvent,
- AppleEvent &outAEReply,
- AEDesc &outResult,
- long inAENumber)
- {
- switch (inAENumber) {
-
- case ae_pict2ascii:
- DoConvertPict2Ascii(inAppleEvent, outAEReply);
- break;
-
- default:
- LDocApplication::HandleAppleEvent(inAppleEvent, outAEReply,
- outResult, inAENumber);
- break;
- }
- }
-
- // ---------------------------------------------------------------------------------
- // • GetAEProperty
- // ---------------------------------------------------------------------------------
-
- void
- CPict2AsciiApp::GetAEProperty(
- DescType inProperty,
- const AEDesc &inRequestedType,
- AEDesc &outPropertyDesc) const
- {
- const CPreferences* thePrefs = &mPreferences;
- OSErr err;
-
- switch (inProperty) {
-
- case pTextFont: {
- TextTraitsRecord theTextTraits;
- thePrefs->GetTextTraits(theTextTraits);
- err = ::AECreateDesc(typeChar, &(theTextTraits.fontName)[1], StrLength(theTextTraits.fontName), &outPropertyDesc);
- ThrowIfOSErr_(err);
- break;
- }
- case pTextPointSize: {
- TextTraitsRecord theTextTraits;
- thePrefs->GetTextTraits(theTextTraits);
- err = ::AECreateDesc(typeShortInteger, &theTextTraits.size, sizeof(theTextTraits.size), &outPropertyDesc);
- ThrowIfOSErr_(err);
- break;
- }
- case pZoomFactor:
- ThrowOSErr_(errAEEventNotHandled); // ### not yet
- break;
-
- case pAutoContrast: {
- Boolean theAutoContrast = thePrefs->GetContrast();
- err = ::AECreateDesc(typeBoolean, &theAutoContrast, sizeof(theAutoContrast), &outPropertyDesc);
- ThrowIfOSErr_(err);
- break;
- }
- case pOptimizedForPrint: {
- Boolean thePrintOpt = !(thePrefs->GetScreen());
- err = ::AECreateDesc(typeBoolean, &thePrintOpt, sizeof(thePrintOpt), &outPropertyDesc);
- ThrowIfOSErr_(err);
- break;
- }
- case pAllow8Bit: {
- Boolean theAllow8Bit = !(thePrefs->Get7Bits());
- err = ::AECreateDesc(typeBoolean, &theAllow8Bit, sizeof(theAllow8Bit), &outPropertyDesc);
- ThrowIfOSErr_(err);
- break;
- }
- case pCharSet: {
- LStr255 theCharSet = thePrefs->GetAllowed();
- err = ::AECreateDesc(typeChar, StringPtr(theCharSet) + 1, theCharSet.Length(), &outPropertyDesc);
- ThrowIfOSErr_(err);
- break;
- }
- case pMonoSpace: {
- Boolean theMonoSpace = thePrefs->GetMonoSpace();
- err = ::AECreateDesc(typeBoolean, &theMonoSpace, sizeof(theMonoSpace), &outPropertyDesc);
- ThrowIfOSErr_(err);
- break;
- }
-
- default:
- LDocApplication::GetAEProperty(inProperty, inRequestedType,
- outPropertyDesc);
- break;
- }
- }
-
-
- // ---------------------------------------------------------------------------------
- // • SetAEProperty
- // ---------------------------------------------------------------------------------
-
- void
- CPict2AsciiApp::SetAEProperty(
- DescType inProperty,
- const AEDesc& inValue,
- AEDesc& outAEReply)
- {
- CPreferences* thePrefs = &mPreferences;
-
- switch (inProperty)
- {
- case pTextFont: {
- Str255 theFontName;
- short theFontNumber;
- TextTraitsRecord theTextTraits;
- UExtractFromAEDesc::ThePString(inValue, theFontName);
- ::GetFNum(theFontName, &theFontNumber);
- if (theFontNumber == 0) {
- ThrowOSErr_(paramErr);
- }
- else {
- thePrefs->GetTextTraits(theTextTraits);
- BlockMoveData(theFontName, theTextTraits.fontName, StrLength(theFontName) + 1);
- theTextTraits.fontNumber = theFontNumber;
- thePrefs->SetTextTraits(theTextTraits);
- //### UpdateWindows();
- }
- break;
- }
- case pTextPointSize: {
- short theFontSize;
- TextTraitsRecord theTextTraits;
- thePrefs->GetTextTraits(theTextTraits);
- UExtractFromAEDesc::TheInt16(inValue, theFontSize);
- theTextTraits.size = theFontSize;
- thePrefs->SetTextTraits(theTextTraits);
- //### UpdateWindows();
- break;
- }
- case pZoomFactor:
- ThrowOSErr_(errAEEventNotHandled); // ### not yet
- break;
-
- case pAutoContrast: {
- Boolean theAutoContrast;
- UExtractFromAEDesc::TheBoolean(inValue, theAutoContrast);
- thePrefs->SetContrast(theAutoContrast);
- //### UpdateWindows();
- break;
- }
- case pOptimizedForPrint: {
- Boolean thePrintOpt;
- UExtractFromAEDesc::TheBoolean(inValue, thePrintOpt);
- thePrefs->SetScreen(!thePrintOpt);
- //### UpdateWindows();
- break;
- }
-
- case pAllow8Bit: {
- Boolean theAllow8Bit;
- UExtractFromAEDesc::TheBoolean(inValue, theAllow8Bit);
- thePrefs->Set7Bits(!theAllow8Bit);
- //### UpdateWindows();
- break;
- }
-
- /* ### not yet
- case pCharSet: {
- break;
- }
- case pMonoSpace: {
- Boolean theMonoSpace;
- UExtractFromAEDesc::TheBoolean(inValue, theMonoSpace);
- thePrefs->SetMonoSpace(theMonoSpace);
- break;
- }
- */
-
- default:
- LDocApplication::SetAEProperty(inProperty, inValue, outAEReply);
- }
- }
-
- // ---------------------------------------------------------------------------------
- // • ConvertPict2Ascii
- // ---------------------------------------------------------------------------------
-
- void
- CPict2AsciiApp::DoConvertPict2Ascii(
- const AppleEvent &inAppleEvent,
- AppleEvent& outAEReply)
- {
- FSSpec theFSSpec;
- CDocument* theDoc;
- OSErr err;
-
- {
- DescType actualType;
- Size actualSize;
-
- err = ::AEGetParamPtr(&inAppleEvent, keyDirectObject, typeFSS, &actualType, &theFSSpec, sizeof(theFSSpec), &actualSize);
- ThrowIfOSErr_(err);
- }
-
- theDoc = new CDocument (this, &theFSSpec, &mPreferences, false); // create an invisible document
-
- Handle theTextH = (theDoc->mGenText).GetText();
- if ( theTextH==nil )
- theTextH = (theDoc->mDynamicText)->GetTextHandle();
-
- {
- StHandleLocker theLock(theTextH);
- err = ::AEPutParamPtr(&outAEReply, keyAEResult, typeChar, *theTextH, ::GetHandleSize(theTextH));
- }
-
- theDoc->Close();
-
- ThrowIfOSErr_(err);
- }
-